﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Sklad1_2.Add.Update;
using Sklad1_2.Data;
using Sklad1_2.Default;

namespace Sklad1_2.Search
{
    public partial class SearchZakupka : Defaultform
    {
        private bool flag;
        
        public SearchZakupka()
        {
            InitializeComponent();
            Init();
        }

        public override sealed void Init()
        {
            ABase.AddItemDBGridView(dataGridView1, Helper.QueryZakupka);
            Helper.Hidden(dataGridView1, 0, 1, 2, 3, 4);
            string q1 = "select sum(summa) from sklad where id=id";
            string q2 = "select sum(ostatok) from sklad where id=id";
            toolStripStatusLabel1.Text = "Сумма=" + ABase.SelectFieldQ(q1);
            toolStripStatusLabel2.Text = "Остаток=" + ABase.SelectFieldQ(q2);
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            flag = dateTimePicker1.Enabled = dateTimePicker2.Enabled = checkBox1.Checked;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string query = Helper.QueryZakupka;
            string q1 = "select sum(summa) from sklad where id=id";
            string q2 = "select sum(ostatok) from sklad where id=id";
            if (flag)
            {
                query += " and a.datec between cdate('" + dateTimePicker1.Text + "') and cdate('" + dateTimePicker2.Text +"')";
                q1 += " and datec between cdate('" + dateTimePicker1.Text + "') and cdate('" + dateTimePicker2.Text + "')";
                q2 += " and datec between cdate('" + dateTimePicker1.Text + "') and cdate('" + dateTimePicker2.Text + "')";
            }
            ABase.AddItemDBGridView(dataGridView1,query);
            toolStripStatusLabel1.Text ="Сумма="+ ABase.SelectFieldQ(q1);
            toolStripStatusLabel2.Text = "Остаток"+ ABase.SelectFieldQ(q2);
            Helper.Hidden(dataGridView1, 0, 1, 2, 3, 4);
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1)
            {
                return;
            }
            data=new List<string>();
            Helper.Db2List(dataGridView1, e, ref data, 0, 1, 2, 3, 4, 9, 10, 11, 12, 13);
            UpdateZakupka aZakupka=new UpdateZakupka(data);
            aZakupka.FormClosed += Bind;
            aZakupka.ShowDialog();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex==-1 || e.ColumnIndex==-1)
            {
                return;
            }
            toolStripStatusLabel1.Text = "Сумма=" + Convert.ToString(dataGridView1[11, e.RowIndex].Value);
            toolStripStatusLabel2.Text = "Остаток=" + Convert.ToString(dataGridView1[13, e.RowIndex].Value);
            string quer = Helper.QueryRas1;
            ABase.AddItemDBGridView(dataGridView2, quer += " and a.zak_id=" + Convert.ToString(dataGridView1[0, e.RowIndex].Value));

        }

        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1)
            {
                return;
            }
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1)
            {
                return;
            }
        }

    }
}
